package com.map.application.headquarter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale.Category;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import com.map.database.Database;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.JSplitPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SearchMap extends JFrame {

	private JPanel contentPane;
	private JTextField searchTextFeild;
	private JTextField txtTests;
	private JTextField textField_1;

	public SearchMap() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(AddMap.class.getResource("/resources/earth_icon.png")));
		setTitle("Search Map");
		setBounds(100, 100, 450, 200);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);

		panel.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("25dlu:grow(4)"), FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("25dlu:grow(4)"), FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, }));

		List<String> searchCategory = new ArrayList<String>();
		searchCategory.add("Map Id");
		searchCategory.add("Map Name");
		searchCategory.add("Map Type");
		searchCategory.add("Cost Price");
		searchCategory.add("Selling Price");
		searchCategory.add("Quantity");

		JLabel selectCategoryLabel = new JLabel("Select Category");
		panel.add(selectCategoryLabel, "10, 8, left, center");

		final JComboBox categoryComboBox = new JComboBox();
		categoryComboBox.setModel(new DefaultComboBoxModel(searchCategory.toArray()));

		panel.add(categoryComboBox, "12, 8, fill, fill");

		JLabel searchLabel = new JLabel("Search ");
		panel.add(searchLabel, "10, 12, left, center");

		searchTextFeild = new JTextField();
		panel.add(searchTextFeild, "12, 12, fill, fill");
		searchTextFeild.setColumns(10);
		

		JButton searchButton = new JButton("Search");
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String whereClause = "";
					if (categoryComboBox.getSelectedIndex() == 0){
						whereClause = "mapId = "+ searchTextFeild.getText();
					}else if(categoryComboBox.getSelectedIndex() == 1){
						whereClause = "mapName = '"+ searchTextFeild.getText()+"'";
					}else if(categoryComboBox.getSelectedIndex() == 2){
						whereClause = "mapType = '"+ searchTextFeild.getText()+"'";
					}else if(categoryComboBox.getSelectedIndex() == 3){
						whereClause = "costPrice = "+ searchTextFeild.getText();
					}else if(categoryComboBox.getSelectedIndex() == 4){
						whereClause = "sellingPrice = "+ searchTextFeild.getText();
					}else{
						whereClause = "quantity = "+ searchTextFeild.getText();
					}
					HeadQuarterMainWindow.updateMapTableOnCondition("map", whereClause);
					dispose();

				} catch (TableEmptyException e1) {
					JOptionPane.showMessageDialog(null, "Nothing Found !!", "alert", JOptionPane.ERROR_MESSAGE);
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					JOptionPane.showMessageDialog(null, "Error from Database !!", "alert", JOptionPane.ERROR_MESSAGE);
					e1.printStackTrace();
				}
			}
		});
		panel.add(searchButton, "10, 16, fill, center");

		JButton cancelButton = new JButton("Clear");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				categoryComboBox.setSelectedIndex(0);
				searchTextFeild.setText("");
			}
		});
		panel.add(cancelButton, "12, 16, fill, fill");
	}

}
